package cn.melon.mod.roll;

import java.util.List;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.melon.Helper.LoggerHelper;
import cn.melon.base.BaseController;
import cn.melon.mod.login.UserInfo;
import cn.melon.mod.sc.IStDao;
import cn.melon.mod.sc.IStDaolmpl;
import cn.melon.mod.sc.StInfo;

/**
 * RollController.java.
 * 
 * 处理请求, 远程发过来的请求会保存在request变量中
 * 
 * @author corvo
 * @date Aug 26, 2016
 */
public class RollController extends BaseController {

    public String TAG = LoggerHelper.makeLogTag(RollController.class);
    public Logger rollLogger = LoggerHelper.serverLogger();

    private IRollDao rollDao = null;

    public RollController(String request) {
        super(request);
        rollDao = new IRollDaoImpl();
    }

    @Override
    public void onResponse() {
        String req = getRequest();
        UserInfo userInfo = getUserInfo();

        rollLogger.debug(TAG + req);            // 和返回值有关的请使用log.debug
       
        try {
            JSONObject reqJSON = new JSONObject(req);
            String action = reqJSON.getString("action");
            RollInfo info = null;
            switch (action) {
                    
                case "ASKINFO":                 // 查询学籍信息
                    setResponse(askInfo());
                    break;
                    
                case "DELROLL":                 // 删除学籍信息(管理员)
                    String studentNumber =  reqJSON.getString("content");
                    setResponse(delRoll(studentNumber));
                    break;
                
                case "ADDROLL":                 // 添加学生信息(管理员)
                    info = RollInfo.toInfo(
                            reqJSON.getJSONObject("content"));
                    setResponse(addRoll(info));
                    break;
                
                case "UPDATEROLL":               // 修改学生信息
                    info = RollInfo.toInfo(
                            reqJSON.getJSONObject("content"));
                    setResponse(updateRoll(info));
                    break;
                    
                case "UPDATEROLLST":
                    info = RollInfo.toInfo(reqJSON.getJSONObject("content"));
                    StInfo stInfo = StInfo.toInfo(reqJSON.getJSONObject("content_st"));
                    setResponse(updateRollSt(info, stInfo));
                    break;
                case "ALLROLL":
                    setResponse(allRoll());
                    break;

                default:
                    setResponse("{}");
                    break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
    }

    private String updateRollSt(RollInfo info, StInfo stInfo) {
        JSONObject jRet = new JSONObject();
        IStDao stDao = new IStDaolmpl();
        stDao.deleteOneStudent(stInfo.getStudentNumber());
        try {
            if (rollDao.updateInfo(info) && stDao.addOneStudent(stInfo)) {
                jRet.put("code", 200);
                jRet.put("content", "更新信息成功");
            } else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jRet.toString();
    }

    /**
     * @Title: allRoll
     * @Description: ALLROLL的请求处理
     * @return String
     */
    private String allRoll() {

        JSONObject jRet = new JSONObject();
        try {
            List<RollInfo> infoList = rollDao.getAllRollInfo();
            JSONArray jContent = new JSONArray();
            if (infoList != null) {
                
                for(RollInfo info : infoList) {
                    jContent.put(info.toJSON());
                }
                
                jRet.put("content", jContent);
                jRet.put("code", 200);
            } else {
                jRet.put("code", 300);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jRet.toString();
    }

    /**
     * @Title: askInfo
     * @Description: ASKINFO的请求处理
     * @return String
     */
    private String askInfo() {

        RollInfo info = rollDao.getInfo(getUserInfo().getStudentNum());

        JSONObject jRet = new JSONObject();
        try {
            if (info != null) {
                jRet.put("content", info.toJSON());
                jRet.put("code", 200);
            } else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jRet.toString();
    }

    /**
     * @Title: delRoll
     * @Description: DELROLL的请求处理
     * @param studentNumber
     * @return String
     */
    private String delRoll(String studentNumber) {
        JSONObject jRet = new JSONObject();
        try {
            if (rollDao.deleteInfo(studentNumber)) {
                jRet.put("code", 200);
                jRet.put("content", "删除信息成功");
            } else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jRet.toString();
    }

    /**
     * @Title: addRoll
     * @Description: ADDROLL的请求处理
     * @param info
     * @return String
     */
    private String addRoll(RollInfo info) {
    	JSONObject jRet = new JSONObject();
        try {
            if (rollDao.addInfo(info)) {
                jRet.put("code", 200);
                jRet.put("content", "添加信息成功");
            } else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jRet.toString();
    }

    /**
     * @Title: updateRoll
     * @Description: UPDATEROLL的请求处理
     * @param info
     * @return String
     */
    private String updateRoll(RollInfo info) {
        
        JSONObject jRet = new JSONObject();
        try {
            if (rollDao.updateInfo(info)) {
                jRet.put("code", 200);
                jRet.put("content", "更新信息成功");
            } else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jRet.toString();
    }
}
